if select(2, UnitClass('player')) ~= "SHAMAN" then
	return
end
local elements = {"Earth", "Air", "Water", "Fire"}

function Yata:InitTotemSets()
	if self.db.profile.TotemSets then
		for i,b in pairs(self.db.profile.TotemSets) do
			self.db.profile.TotemSets[i] = self:FixTotemSet(b)
		end
	else 
		self.db.profile.TotemSets = {}
		self.db.profile.TotemSets["default"] = self:FixTotemSet()
	end
end

function Yata:GetTotemSet(name)
	return self.db.profile.TotemSets[name or self.db.profile.CurrentTotemSet]
end

function Yata:SetTotemSet(name, set)	
	if not name then
		name = self.db.profile.CurrentTotemSet
	end
	if not set then
		set = {}
		set.ElementsOrder = {}
		set.HiddenButtons = {}
		for k,v in ipairs(elements) do
			local header = self.TotemButtons[v].Header		
			local pos = header:GetAttribute("position")
			set.ElementsOrder[pos + 1] = v
			set[v] = {}
			for i, b in ipairs(self.TotemButtons[v].Buttons) do
				set[v][b:GetAttribute("position")+1] = b.Totem.Name
				if b:GetAttribute("hidden") then
					table.insert(set.HiddenButtons, b.Totem.Name)
				end				
			end
		end
	end
	self.db.profile.TotemSets[name] = set
end


function Yata:FixTotemSet(set)
	if not set then
		set = { ["ElementsOrder"] = {} }	
		for k,v in ipairs(elements) do
			set[v] = {}
			set.ElementsOrder[k] = v	
		end
	elseif	#set.ElementsOrder ~= #elements then -- error case
		set.ElementsOrder = elements
	end
	-- removalTable
	local goodTable = {}
	for k,v in ipairs(elements) do
		goodTable[v] = {}
	end
	for k,v in ipairs(Yata.TotemData) do
		local totem = Yata.TotemData[v]
		totem = totem[#totem]
		local found = #goodTable[totem.Element] + 1
		for m,n in ipairs(set[totem.Element] or {}) do
			if n == totem.Name then
				found = m
				break
			end
		end
		table.insert(goodTable[totem.Element],found,totem.Name)
	end	
	for k,v in ipairs(elements) do
		set[v] = {}
		for m,n in pairs(goodTable[v]) do -- pairs here because we can't guarantee that it actually has no gaps (in some error cases)
			table.insert(set[v], n)
		end
	end
	return set
end

function Yata:ChangeTotemSet(name)
	local set = self:GetTotemSet(name)
	if set then
		for k,v in ipairs(set.ElementsOrder) do
			self.TotemButtons[v].Header:SetAttribute("position",k-1)
			for i,b in ipairs(set[v]) do
				for m,n in ipairs(self.TotemButtons[v].Buttons) do
					if n.Totem.Name == b then
						n:SetAttribute("newposition",i-1)
					end
				end
			end
		end
		local hidden = set.HiddenButtons
			if hidden then
			for k,v in ipairs(hidden) do
				for i,b in ipairs(self.TotemButtons.Buttons) do
					if v == b.Totem.Name then
						b:SetAttribute("hidden",true)
						break
					end
				end
			end
		end
	self.db.profile.CurrentTotemSet = name
	end
end
